using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Security;
using LoveMusic.Helpers;
using LoveMusic.Models;

namespace LoveMusic
{
    [OutputCache(Duration = int.MaxValue, VaryByParam = "artistName")]
    public class ArtistController : BaseController, IDisposable
    {
        #region private

        private readonly IDataContext _dataContext;
        private static readonly int _topTags = 50;

        private IDataContext DataContext
        {
            [System.Diagnostics.DebuggerStepThrough]
            get
            {
                return _dataContext;
            }
        }

        /// <summary>
        /// Initializes the <see cref="SongController"/> class.
        /// </summary>
        static ArtistController()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SongController"/> class.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="userManager">The membership provider that will be used.</param>
        public ArtistController(IDataContext dataContext, MembershipProvider userManager)
            : base(userManager)
        {
            _dataContext = dataContext;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SongController"/> class.
        /// </summary>
        public ArtistController()
            : this(new ModelDataContext(), Membership.Provider)
        {
        }

        #endregion

        /// <summary>
        /// Get the artist
        /// </summary>
        /// <param name="artistName">The artistName (Mandatory).</param>
        [CompressFilter]
        public ActionResult Index(string artistName)
        {
            string originalArtistName = artistName;
            artistName = artistName.Replace("+", " ");
            int counterLimit = 0;
            int counter = 0;
            
            var viewData = GetViewData<ArtistDetailData>();
            ArtistDetailItem artistDetailItem = DataContext.GetArtistDetailByName(CurrentUserId, artistName);
            if (artistDetailItem != null)
            {
                if (artistDetailItem.NameVariationsFromDatabase != string.Empty)
                    artistDetailItem.NameVariations = WebHelper.SplitString(artistDetailItem.NameVariationsFromDatabase.Substring(1, artistDetailItem.NameVariationsFromDatabase.Length - 1), "|");
                 

                #region mp3 and youtube

                artistDetailItem.Mp3Songs = new Mp3SongBin();
                artistDetailItem.PlayListYouTube = new List<string>();

                foreach (SongSmallBin songSmallBin in artistDetailItem.Songs)
                {
                    if (counter == 0 && songSmallBin.MP3FileID != null && songSmallBin.MP3FileID != 0)
                    { // first song
                        artistDetailItem.Mp3Songs.Url = "/Song/GetStreamMp3/" + songSmallBin.MP3FileID.ToString() + "/" + songSmallBin.StoryID.ToString();
                        artistDetailItem.Mp3Songs.Title = WebHelper.RemoveSpecialCharactersExceptTire(songSmallBin.name);
                        counter++;
                    }else if (songSmallBin.MP3FileID != null && songSmallBin.MP3FileID != 0)
                    {
                        artistDetailItem.Mp3Songs.Url = artistDetailItem.Mp3Songs.Url + "|" + "/Song/GetStreamMp3/" + songSmallBin.MP3FileID.ToString() + "/" + songSmallBin.StoryID.ToString();
                        artistDetailItem.Mp3Songs.Title = artistDetailItem.Mp3Songs.Title + "|" + WebHelper.RemoveSpecialCharactersExceptTire(songSmallBin.name);
                        counter++;
                    }

                    if (songSmallBin.YouTubeID != null && songSmallBin.YouTubeID != string.Empty && counterLimit < 100 && !artistDetailItem.PlayListYouTube.Contains(songSmallBin.YouTubeID))
                    {
                        counterLimit++;
                        artistDetailItem.PlayListYouTube.Add(songSmallBin.YouTubeID);
                    }
                }

                #endregion

                #region Blogs

                foreach (Review review in artistDetailItem.Reviews)
                {
                    review.Summary = review.Summary.Replace("&lt;", "<");
                    review.Summary = review.Summary.Replace("&gt;", ">");
                    review.Summary = review.Summary.Replace("<span>", "");
                    review.Summary = review.Summary.Replace("</span>", "");
                    review.Summary = review.Summary.Replace(artistDetailItem.Name, "<a href=\"/artist/" + originalArtistName + "\" style=\"font-size:smaller\" >" + artistDetailItem.Name + "</a>");
                }

                foreach (Blog blog in artistDetailItem.Blogs)
                {
                    blog.Summary = blog.Summary.Replace("&lt;", "<");
                    blog.Summary = blog.Summary.Replace("&gt;", ">");
                    blog.Summary = blog.Summary.Replace("<span>", "");
                    blog.Summary = blog.Summary.Replace("</span>", "");
                    blog.Summary = blog.Summary.Replace(artistDetailItem.Name, "<a href=\"/artist/" + originalArtistName + "\" style=\"font-size:smaller\" >" + artistDetailItem.Name + "</a>");

                }

                foreach (Bio bio in artistDetailItem.Bios)
                {
                    bio.Text = bio.Text.Replace("&lt;", "<");
                    bio.Text = bio.Text.Replace("&gt;", ">");
                    bio.Text = bio.Text.Replace("<span>", "");
                    bio.Text = bio.Text.Replace("</span>", "");
                    bio.Text = bio.Text.Replace(artistDetailItem.Name, "<a href=\"/artist/" + originalArtistName + "\" style=\"font-size:smaller\" >" + artistDetailItem.Name + "</a>");
                }

                #endregion

                viewData.Artist = artistDetailItem;
                return View("Index", viewData);
            }
            else
                return View("NotFound", viewData);

        }

        /// <summary>
        /// Get the artist or create it
        /// </summary>
        /// <param name="artistName">The artistName (Mandatory).</param>
        [CompressFilter]
        public ActionResult SimilarArtist(string artistName)
        {
            string originalArtistName = artistName;
            artistName = artistName.Replace("+", " ");
            var viewData = GetViewData<ArtistDetailData>();
            ARTIST art = DataContext.GetArtistByName(artistName);
            if (art == null)
            {
                ArtistDetailItem artist = DataContext.CreateArtistFromSimilar(CurrentUserId, artistName);
                return RedirectToAction("Index", new { artistName = originalArtistName });
            }
            else
                return RedirectToAction("Index", new { artistName = originalArtistName });
        }

        #region Utilities

        private T GetViewData<T>() where T : BaseViewData, new()
        {
            var viewData = new T
            {
                IsAuthenticated = IsUserAuthenticated,
                UserName = CurrentUserName,
                Categories = DataContext.GetCategories(),
                Tags = DataContext.GetTags(_topTags),
                AllMoods = DataContext.AllMoods(),
                RandArtists = DataContext.Get50Artist(),
                RandSong = DataContext.Get50Songs()
            };

            return viewData;
        }

        #endregion
    }
}
